/**
 * 全局导航跳转工具类
 */
export default class NavigationUtil {
    /**
     * 跳转到指定页面
     * @param params 要传递的参数
     * @param page 要跳转的页面名
     **/
    static goPage(page, params) {
        const navigation = NavigationUtil.navigation;
        if (!navigation) {
            console.log('NavigationUtil.navigation can not be null');
            return;
        }
        navigation.navigate(page, { ...params });
    }

    /**
     * 跳转到指定页面 -- 无返回
     * @param params 要传递的参数
     * @param page 要跳转的页面名
     */
    static replacePage(page, params) {
        const navigation = NavigationUtil.navigation;
        if (!navigation) {
            console.log('NavigationUtil.navigation can not be null');
            return;
        }
        navigation.replace(page, { ...params });
    }

    /**
     * 返回上一页
     * @param navigation
     */
    static goBack(navigation) {
        navigation.goBack();
    }

    /**
     * 重置到首页
     * @param navigation
     */
    static resetToHomPage(params) {
        const { navigation } = params;
        navigation.replace('Home');
    }

    /**
     * 页面将要获取焦点执行的函数
     * @param callback
     */
    static willFocus(callback) {
        const navigation = NavigationUtil.navigation;
        if (!navigation) {
            console.log('NavigationUtil.navigation can not be null');
            return;
        }
        navigation.addListener('willFocus', () => {
            callback();
        });
    }

    /**
     * 页面获取焦点后执行的函数
     * @param callback
     */
    static didFocus(callback) {
        const navigation = NavigationUtil.navigation;
        if (!navigation) {
            console.log('NavigationUtil.navigation can not be null');
            return;
        }
        navigation.addListener('didFocus', () => {
            callback();
        });
    }
    /**
     * 页面将要失去焦点执行的函数
     * @param callback
     */
    static willBlur(callback) {
        const navigation = NavigationUtil.navigation;
        if (!navigation) {
            console.log('NavigationUtil.navigation can not be null');
            return;
        }
        navigation.addListener('willBlur', () => {
            callback();
        });
    }
    /**
     * 页面失去焦点后执行的函数
     * @param callback
     */
    static didBlur(callback) {
        const navigation = NavigationUtil.navigation;
        if (!navigation) {
            console.log('NavigationUtil.navigation can not be null');
            return;
        }
        navigation.addListener('didBlur', () => {
            callback();
        });
    }
}
